//
//  WaringViewController.swift
//  Box
//
//  Created by mn on 2025/4/3.
//

import UIKit

class WaringViewController: UIViewController, UIGestureRecognizerDelegate {
    
    private let waringView = WaringView()

    override func viewDidLoad() {
        super.viewDidLoad()
        setupView()
    }
    
    private func setupView() {
        view.backgroundColor = UIColor.clear
        
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissController))
        view.addGestureRecognizer(tapGesture)
                
        view.addSubview(waringView)
        waringView.snp.makeConstraints { make in
            make.bottom.equalToSuperview()
            make.left.right.equalToSuperview()
        }
        
        tapGesture.delegate = self
        waringView.button.addAction {
            [weak self] in
            self?.alert()
        }
    }
    func alert() {
        let alert = UIAlertController(title: "确认注销", message: "注销账户后将无法恢复，确定要继续吗？", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
        alert.addAction(UIAlertAction(title: "确认", style: .destructive, handler: { [weak self] _ in
            APIService.request(api: .deleteUser) {
                [weak self] (result,_) in
                if result.success {
                    UserManager.shared.clearUser()
                    self?.navigationController?.pop()
                } else {
                    self?.toast(result.message)
                }
            }
        }))
        present(alert, animated: true, completion: nil)

    }
    
    @objc private func dismissController() {
        dismiss(animated: true, completion: nil)
    }
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        return !waringView.frame.contains(touch.location(in: view))
    }
}
